package cn.zhaoyuening.zshop4j.manager.web;

import cn.zhaoyuening.zshop4j.common.bean.EasyUiListResult;
import cn.zhaoyuening.zshop4j.common.bean.ZShop4jResponceResult;
import cn.zhaoyuening.zshop4j.common.utils.JsonUtils;
import cn.zhaoyuening.zshop4j.content.service.ContentService;
import cn.zhaoyuening.zshop4j.manager.pojo.TbContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Zhao on 2017/4/15.
 */
@Controller
public class ContentController {
    @Autowired
    private ContentService contentService;

    @RequestMapping(value = "contents", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String findByCategoryId(@RequestParam long categoryId,@RequestParam("page") int pageNum,@RequestParam("rows") int pageSize) {
        EasyUiListResult<TbContent> easyUiListResult = contentService.findByCategoryId(categoryId, pageNum, pageSize);
        String jsonData = JsonUtils.objectToJson(easyUiListResult);
        return jsonData;
    }

    @RequestMapping(value = "content", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String insert(TbContent content) {
        ZShop4jResponceResult result = contentService.insert(content);
        String jsonData = JsonUtils.objectToJson(result);
        return jsonData;
    }

    @RequestMapping(value = "content", method = RequestMethod.DELETE, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteByIdList(Long[] ids,Long categoryId) {
        ZShop4jResponceResult result = contentService.deleteByIdList(ids,categoryId);
        String jsonData = JsonUtils.objectToJson(result);
        return jsonData;
    }

    @RequestMapping(value = "content/{id}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String findById(@PathVariable("id") long id) {
        TbContent content = contentService.findById(id);
        String jsonData = JsonUtils.objectToJson(content);
        return jsonData;
    }

    @RequestMapping(value = "content", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String update(TbContent content) {
        content.setUpdated(new Date());
        ZShop4jResponceResult result = contentService.updateContent(content);
        String jsonData = JsonUtils.objectToJson(result);
        return jsonData;
    }

}
